<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\Cate;
use app\common\model\Example;
use app\common\model\Picture;
use app\common\model\Question;
use app\common\model\Technology;


class Index
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        return sucessMsg('200', '获取成功', [
            'sucai1' => $this->sucai1(),//获取首页素材1
            'wallpaper' => $this->wallpaper(),//获取壁纸专题2
            'example2' => $this->example2(),//获取实例专题3
            'quesyion' => $this->question(),//获取问答下小分类
            'sucai' => $this->sucai(),//获取素材模板专题
            'example' => $this->example(),//获取优秀实例专题
            'technology' => $this->technology(),//获取首页代码知识，技术社区左侧
            'technology1' => $this->technology1(),//获取首页代码知识，技术社区右侧
        ]);
    }


    /**
     * 获取首页素材1
     */
    public function sucai1()
    {
        return Picture::where('status', 1)
            ->field('id,title,image,param')
            ->order('id', 'asc')
            ->limit(0, 1)
            ->withAttr('image', function ($val, $date) {
                return env('app.yuming') . $val;
            })
            ->withAttr('param', function ($val1, $data) {
                return json_decode($val1);
            })
            ->select();
    }

    /**
     * 获取壁纸专题2
     */
    public function wallpaper()
    {
        return Picture::where('status', 1)
            ->field('id,title,image')
            ->order('id', 'asc')
            ->limit(1, 1)
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
    }

    /**
     * 获取首页实例3
     */
    public function example2()
    {
        return Example::where('status', 1)
            ->field('id,title,image')
            ->order('id', 'asc')
            ->limit(0, 1)
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
    }

    /**
     * 获取首页问答分类
     */
    public function question()
    {
        return Cate::where(['status' => 1, 'type' => 3])
            ->field('id,name,created_at')
            ->order('sort', 'asc')
            ->select();
    }


    /**
     * 获取素材模板专题
     */
    public function sucai()
    {
        return Picture::where('status', 1)
            ->field('id,image')
            ->order('id', 'desc')
            ->limit(0, 6)
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
    }


    /**
     * 获取首页实例3
     */
    public function example()
    {
        return Example::where('status', 1)
            ->field('id,image')
            ->order('id', 'desc')
            ->limit(0, 6)
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
    }

    /**
     * 获取代码知识，技术社区左侧
     */
    public function technology()
    {
        return Technology::where('status', 1)
            ->field('id,title,created_at')
            ->order('id', 'desc')
            ->limit(0, 7)
            ->select();
    }

    /**
     * 获取代码知识，技术社区右侧
     */
    public function technology1()
    {
        return Technology::where('status', 1)
            ->field('id,title,company,image,created_at')
            ->order('id', 'desc')
            ->limit(7, 4)
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
    }

    /**
     * 获取首页问答
     */
    public function questions()
    {
        $cate_id = input('id/d');
        $id = Cate::where(['status' => 1, 'type' => 3])
            ->order('id', 'asc')
            ->limit(0, 1)
            ->value('id');
        $where['status'] = 1;
        if (empty($cate_id)) {
            $where['cate_id'] = $id;
        } else {
            $where['cate_id'] = $cate_id;
        }
        $quesyions = Question::where($where)
            ->field('id,headtitle,content,picture,created_at')
            ->order('sort', 'asc')
            ->limit(0, 4)
            ->withAttr('picture', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
        return sucessMsg('200', '获取成功', $quesyions);
    }

    /**
     *获取问答详情
     */
    public function details()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(422,'无id查询');
        }
        $details = Question::where(['status' => 1, 'id' => $id])
            ->field('id,title,name,label,content,image,zan_num,comment_num,collect_num,created_at')
            ->select();
        if (empty($details)) {
            return errMsg(422,'页面丢失，请稍后再试！');
        }
        foreach ($details as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
            $v['created_at'] = date('Y-m-d H:i:s');
        }
        return sucessMsg('200', '获取成功', $details);
    }

}
